package com.invoicing.controller;

import com.invoicing.MainController;
import com.invoicing.Service.*;
import com.invoicing.entity.Commodity;
import com.invoicing.entity.CommodityData;
import com.invoicing.entity.OrderItem;
import com.invoicing.entity.VendorItem;
import com.invoicing.view.MenuView;
import de.felixroske.jfxsupport.FXMLController;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

@FXMLController
public class MessageController implements Initializable {


    @Autowired
    private VendService vendService;
    @Autowired
    private VenditemService venditemService;
    @Autowired
    CommodityService commodityService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderitemService orderitemService;
    @FXML
    private TextField vend_id;
    @FXML
    private TableView table_vend;
    @FXML
    private TableColumn vend_name;
    @FXML
    private TableColumn vend_price;
    @FXML
    private TableColumn vend_count;
    @FXML
    private TableColumn vend_num;

    @FXML
    private TableView table_sell;
    @FXML
    private TableColumn sell_num;
    @FXML
    private TableColumn sell_name;
    @FXML
    private TableColumn sell_price;
    @FXML
    private TableColumn sell_count;

    @FXML
    private DatePicker vendDate;
    @FXML
    private DatePicker sellDate;



    private List<VendorItem> vendorItemList = new ArrayList<VendorItem>();
    private List<OrderItem> orderItemList = new ArrayList<OrderItem>();
    private ObservableList<CommodityData> data = FXCollections.observableArrayList();
    private ObservableList<CommodityData> data_order = FXCollections.observableArrayList();

    private SimpleDateFormat  dateformat = new SimpleDateFormat("yyyy-MM-dd");



    @FXML
    private void showVend(){
        int id = 0;
        int i = 0;
        float total = 0;
        Date date = new Date();
        try{
            date = dateformat.parse(vendDate.getValue().toString());
        }catch (Exception e){

        }
        data.clear();
        table_vend.setItems(data);
        table_vend.refresh();

        vendorItemList = venditemService.selectByDate(date);

        for(i=0;i<vendorItemList.size();i++){
            Commodity commodity = commodityService.findCommodity(vendorItemList.get(i).getId());
            CommodityData commodityData = new CommodityData(commodity,vendorItemList.get(i).getVendId(),"",vendorItemList.get(i).getVendUnivalence(),vendorItemList.get(i).getVendCount(),0,0);

            data.add(commodityData);
        }
        table_vend.setItems(data);




    }

    @FXML
    private void showOrder(){
        int id = 0;
        int i = 0;
        float total = 0;
        Date date = new Date();
        try{
            date = dateformat.parse(sellDate.getValue().toString());
        }catch (Exception e){

        }
        data_order.clear();
        table_sell.setItems(data);
        table_sell.refresh();

        orderItemList = orderitemService.getByDate(date);

        for(i=0;i<orderItemList.size();i++){
            Commodity commodity = commodityService.findCommodity(orderItemList.get(i).getId());
            CommodityData commodityData = new CommodityData(commodity,orderItemList.get(i).getOrderId(),"",0,0,orderItemList.get(i).getOrderUnivalence(),orderItemList.get(i).getOrderCount());

            data_order.add(commodityData);
        }
        table_sell.setItems(data_order);
    }


    @FXML
    private void setBack(){
        MainController.showView(MenuView.class);
    }
    public void initialize(URL location, ResourceBundle resources) {

        vend_name.setCellValueFactory(new PropertyValueFactory<CommodityData, String>("name"));
        vend_price.setCellValueFactory(new PropertyValueFactory<CommodityData, Float>("vend_price"));
        vend_count.setCellValueFactory(new PropertyValueFactory<CommodityData,Integer>("vend_count"));
        vend_num.setCellValueFactory(new PropertyValueFactory<CommodityData,Integer>("num"));

        sell_name.setCellValueFactory(new PropertyValueFactory<CommodityData, String>("name"));
        sell_price.setCellValueFactory(new PropertyValueFactory<CommodityData, Float>("sell_price"));
        sell_count.setCellValueFactory(new PropertyValueFactory<CommodityData,Integer>("sell_count"));
        sell_num.setCellValueFactory(new PropertyValueFactory<CommodityData,Integer>("num"));
        //discount.setCellValueFactory(new PropertyValueFactory<CommodityData,Float>("discount"));

    }

}
